package com.ruoyi.bizsys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
    import com.ruoyi.bizsys.mapper.RiskAppListMapper;
import com.ruoyi.bizsys.domain.RiskAppList;
import com.ruoyi.bizsys.service.IRiskAppListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 风控AppListService业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
@Slf4j
public class RiskAppListServiceImpl extends ServiceImpl<RiskAppListMapper,RiskAppList> implements IRiskAppListService {

    public static List<RiskAppList> list = new ArrayList<>();

    public static final String RISK_APP_LIST_NUMS = "risk_app_list_nums";

    @Autowired
    RedisCache redisCache;

    /**
     * 查询风控AppList
     *
     * @param id 风控AppListID
     * @return 风控AppList
     */
    @Override
    public RiskAppList selectRiskAppListById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询风控AppList列表
     *
     * @param riskAppList 风控AppList
     * @return 风控AppList
     */
    @Override
    public List<RiskAppList> selectRiskAppListList(RiskAppList riskAppList) {
        return this.list(new QueryWrapper<>(riskAppList).last("order by id desc"));
    }

    /**
     * 新增风控AppList
     *
     * @param riskAppList 风控AppList
     * @return 结果
     */
            @Override
    public int insertRiskAppList(RiskAppList riskAppList) {
                                                                                                return baseMapper.insert(riskAppList);
    }

    /**
     * 修改风控AppList
     *
     * @param riskAppList 风控AppList
     * @return 结果
     */
            @Override
    public int updateRiskAppList(RiskAppList riskAppList) {
                                                                                                return baseMapper.updateById(riskAppList);
    }

    /**
     * 批量删除风控AppList
     *
     * @param ids 需要删除的风控AppListID
     * @return 结果
     */
            @Override
    public int deleteRiskAppListByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除风控AppList信息
     *
     * @param id 风控AppListID
     * @return 结果
     */
    @Override
    public int deleteRiskAppListById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<RiskAppList> getAll() {

        try{
            Integer nums = redisCache.getCacheObject(RISK_APP_LIST_NUMS);
            if(StringUtils.isNull(nums) || nums==0 || this.list.size()!=nums){
                log.info("riskApp本地内存与缓存数据对不上，重新获取.....");
                setRistAppList();
                redisCache.deleteObject(RISK_APP_LIST_NUMS);
                redisCache.setCacheObject(RISK_APP_LIST_NUMS,this.list.size(),1, TimeUnit.DAYS);
            }
        }catch (Exception e){
            log.error("获取riskAppList异常：",e);
            if(this.list.size()==0){
                log.info("redis异常riskApp本地内存为空，重新获取.....");
                setRistAppList();
            }
        }finally {
            return this.list;
        }
    }

    public void setRistAppList(){
        List<RiskAppList> riskAppLists = selectRiskAppListList(new RiskAppList());
        this.list = riskAppLists;
    }


}
